package service;

import java.util.List;

import dao.ProductDao;
import pojo.PageBean;
import pojo.Product;

public class ProductService {
	ProductDao pdao;
	public int updateQuantity(String pid,int quantity) {
		pdao=new ProductDao();
		int flag=pdao.updateQuantity(pid, quantity);
		return flag;
	}
	public PageBean<Product> show(int currentPage,int pageSize) {
		pdao=new ProductDao();
		PageBean<Product> pb = new PageBean<Product>();
		pb.setList(pdao.PageData(currentPage, pageSize));
		pb.setTotal(pdao.getTotal());
		int totalPage=(pdao.getTotal()%pageSize==0)?(pdao.getTotal()/pageSize):(pdao.getTotal()/pageSize+1);
		pb.setPageSize(pageSize);
		pb.setCurrentPage(currentPage);
		pb.setTotalPage(totalPage);
		return pb;
	}
	public List<Product> showAll(int currentPage,int pageSize ){
		pdao=new ProductDao();
		List<Product> list=pdao.PageData(currentPage, pageSize);
		
		return list;
	}
	public int delProduct(String p_id) {
		int flag=0;
		pdao=new ProductDao();
		flag=pdao.del(p_id);
		return flag;
	}
	public int update(Product p) {
		pdao=new ProductDao();
		int flag=pdao.update(p);
		return flag;
	}
	public Product getProduct(String pid) {
		pdao=new ProductDao();
		Product p =pdao.getOneProduct(pid);
		return p;
	}
	public int add(Product p) {
		pdao=new ProductDao();
		int flag=pdao.add(p);
		return flag;
	}
	public PageBean<Product> showAllByType(int currentPage,int pageSize,String type){
		pdao=new ProductDao();
		PageBean<Product> pb=new PageBean<Product>();
		int totle=pdao.getTotal();
		pb.setTotal(totle);
		int totalPage=pdao.getMaxPage(pageSize);
		pb.setTotalPage(totalPage);
		pb.setCurrentPage(currentPage);
		List<Product> list=pdao.showProductByType(currentPage, pageSize, type);
		pb.setList(list);
		return pb;
	}
}
